package com.lanou.zakerproject.city;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ExpandableListView;

/**
 * Created by dllo on 15/12/28.
 */
public class IndexExpandableListView extends View {
    public static final String[] WORD = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private int preHeight = 13;
    private Paint paint;
    private int blankHeight;
    private ExpandableListView expandableListView;
//    private CityAdapter cityAdapter;


    public void setExpandableListView(ExpandableListView expandableListView) {
        this.expandableListView = expandableListView;
//        cityAdapter = (CityAdapter) expandableListView.getExpandableListAdapter();
    }

    public IndexExpandableListView(Context context) {
        super(context);
        init();
    }

    public IndexExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextAlign(Paint.Align.CENTER);//文字的对齐方式是居中
        preHeight = dpToPx(preHeight, getContext());
        paint.setTextSize(preHeight);
        paint.setAntiAlias(true);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //getMeasuredHeight  获得该组件的高度
        blankHeight = (getMeasuredHeight() - (WORD.length * preHeight)) / (WORD.length + 1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < WORD.length; i++) {
            canvas.drawText(WORD[i], getMeasuredWidth() / 2, (i + 1) * (preHeight + blankHeight), paint);

        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //当用户按下的时候
            int index = (int) (event.getY() / (preHeight + blankHeight));
            if (index >= WORD.length) {
                //做一个修正
                index = WORD.length - 1;
            } else if (index < 0) {
                index = 0;
            }
            //通过字母 获得ListView的条数，并滚动到该位置
            //能找到a  但不能实现滚动
//            int pos = cityAdapter.getIndexFromString(WORD[index]);
//            Log.d("IndexView", "pos:" + pos);
//            if (pos >= 0) {
//                expandableListView.setSelectedGroup(pos);//listView滚动到该位置
//            }
        }
        return super.onTouchEvent(event);
    }


    private int dpToPx(int dp, Context context) {
        int px = 0;
        //获得屏幕的dpi，需要通过Resources下的DisplayMetrics的densityDpi
        Resources resources = context.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        px = (int) (dp * displayMetrics.densityDpi / 160f + 0.5f);
        return px;
    }


}
